Version: 1.2 (Jupytext, time measurements, logger, param notebook execution, fixes)
This notebook serves as a documentation notebook for visualisation code from src\visualisations.
import sys
import os
sys.path+=[os.path.join(os.getcwd(), ".."), os.path.join(os.getcwd(), "../..")] # one and two up
ToC Necessary libraries for notebook functionality:
NOTE: This way, using the function, the button works only in active notebook. If the functionality needs to be preserved in html export, then the code has to be incluced directly into notebook.
LOGGER_CONFIG_NAME = "logger_file_limit_console"
ADDAPT_WIDTH = False
try:
from src.utils.notebook_support_functions import create_button, get_notebook_name
NOTEBOOK_NAME = get_notebook_name()
SUPPORT_FUNCTIONS_READ = True
except:
NOTEBOOK_NAME = "NO_NAME"
SUPPORT_FUNCTIONS_READ = False
from src.utils.logger import Logger
from src.utils.envs import Envs
from src.utils.config import Config
from pandas import options
from IPython.display import display, HTML
options.display.max_rows = 500
options.display.max_columns = 500
envs = Envs()
envs.set_logger(LOGGER_CONFIG_NAME)
Logger().start_timer(f"NOTEBOOK; Notebook name: {NOTEBOOK_NAME}")
if ADDAPT_WIDTH:
display(HTML("<style>.container { width:100% !important; }</style>")) # notebook width
2023-12-10 16:11:09,032 - file_limit_console - INFO - Logger was created on WS-3000 in branche 001_repository_set_up. 2023-12-10 16:11:09,033 - file_limit_console - INFO - Process: NOTEBOOK; Notebook name: visualisation_documentation.py; Timer started;
# create_button()
from datetime import datetime
from importlib import reload
from pandas import Series, options, DataFrame
from numpy.random import seed, normal, randint
from numpy import array, log
from numpy.random import randn
from datetime import datetime
from src.utils.date_time_functions import create_datetime_id
from src.visualisations.visualisation_functions import create_time_series
# from src.global_constants import * # Remember to import only the constants in use
N_ROWS_TO_DISPLAY = 2
FIGURE_SIZE_SETTING = {"autosize": False, "width": 2200, "height": 750}
DATA_PROCESSING_CONFIG_NAME = "data_processing_basic"
# MANDATORY FOR CONFIG DEFINITION AND NOTEBOOK AND ITS OUTPUTS IDENTIFICATION #########################################
PYTHON_CONFIG_NAME = "python_local"
ID = create_datetime_id(now=datetime.now(), add_micro=False)
# (END) MANDATORY FOR CONFIG DEFINITION AND NOTEBOOK AND ITS OUTPUTS IDENTIFICATION ###################################
envs.set_config(PYTHON_CONFIG_NAME)
from src.data.attributes import ATTR_OPEN, ATTR_HIGH, ATTR_LOW, ATTR_CLOSE
seed(185)
data = normal(size=500)
print(f"Type of the data is: {type(data)}")
print(f"Several observations: {data[0:10]}")
plot_title = "My Super Random Histogram"
x_title = "Awesome Random Data"
Type of the data is: <class 'numpy.ndarray'> Several observations: [ 0.04471179 -1.09776817 1.63620698 -1.12098251 -0.93842551 -0.71691279 0.77317168 0.31091126 0.67555112 1.00791911]
import src.visualisations.plotly_histogram as HIST
reload(HIST)
histogram = HIST.PlotlyHistogram()
histogram.plot(data=data, plot_title=plot_title, x_title=x_title)
import src.visualisations.plotly_histogram as HIST
reload(HIST)
histogram = HIST.PlotlyHistogram()
histogram.plot(data=data, plot_title=plot_title, x_title=x_title, n_bins=4)
import src.visualisations.plotly_histogram as HIST
reload(HIST)
histogram = HIST.PlotlyHistogram()
histogram.plot(data=data, plot_title=plot_title, x_title=x_title, n_bins=0, x_axis_min_max=(-6, 6))
import src.visualisations.plotly_histogram as HIST
reload(HIST)
histogram = HIST.PlotlyHistogram()
histogram.plot(data=data, plot_title=plot_title, x_title=x_title, n_bins=0, x_axis_min_max=(-6, 6),
vertical_lines_positions=[-1.7, 0.4, 2.33])
array_id = array(["Alpha", "Beta", "Gamma"])
array_values = array([10, 5, 30])
plot_title = "Super Bar Chart"
name_id = "Greek Alphabet"
name_values = "Occurance"
order_by_values = True
reverse = True
import src.visualisations.plotly_bar_chart as BAR
reload(BAR)
bar_chart = BAR.PlotlyBarChart()
bar_chart.plot(array_id, array_values, plot_title, name_id, name_values, order_by_values, reverse)
array_id = array(["Alpha", "Beta", "Gamma"])
array_values = array([10, 5, 30])
plot_title = "Super Bar Chart"
name_id = "Greek Alphabet"
name_values = "Occurance"
order_by_values = False
reverse = False
import src.visualisations.plotly_bar_chart as BAR
reload(BAR)
bar_chart = BAR.PlotlyBarChart()
bar_chart.plot(array_id, array_values, plot_title, name_id, name_values, order_by_values, reverse)
array_id = array([3, 2, 4, 1])
array_values = array([10, 5, 7, 30])
plot_title = "Super Bar Chart"
name_id = "Greek Alphabet"
name_values = "Occurance"
order_by_values = False
import src.visualisations.plotly_bar_chart as BAR
reload(BAR)
bar_chart = BAR.PlotlyBarChart()
bar_chart.plot(array_id, array_values, plot_title, name_id, name_values, order_by_values)
ts_1 = create_time_series(seed_number=11, x_multiplier=0.3)
ts_1_names = [f"Amazing Data {i}" for i in range(1, len(ts_1)+1)]
ts_2 = create_time_series(seed_number=32, x_multiplier=1.1)
ts_2_names = [f"Random Data {i}" for i in range(1, len(ts_2)+1)]
ts_3 = create_time_series(seed_number=64, x_multiplier=1.8)
ts_3_names = [f"Third TS {i}" for i in range(1, len(ts_3)+1)]
# ts_4 = create_time_series(seed_number=20, x_multiplier=0.7)
# ts_5 = create_time_series(seed_number=87, x_multiplier=0.1)
# ts_6 = create_time_series(seed_number=63, x_multiplier=2.5)
print(type(ts_1))
print(type(ts_1[0]))
print(type(ts_1.index))
print("\n")
print(ts_1.head())
plot_title = "Awesome Random Time Series Plot"
y_title = "Awesome Random Data"
<class 'pandas.core.series.Series'> <class 'numpy.float64'> <class 'pandas.core.indexes.datetimes.DatetimeIndex'> 2020-01-01 16.749455 2020-01-02 16.191528 2020-01-03 17.338647 2020-01-04 16.263316 2020-01-05 19.651911 dtype: float64
import src.visualisations.plotly_time_series as TS
reload(TS)
ts_vizu_selectors = TS.PlotlyTimeSeries()
ts_vizu_selectors.set_selectors(selectors=[
{"count": 3, "label": "3d", "step": "day", "stepmode": "backward"},
{"count": 7, "label": "1w", "step": "day", "stepmode": "backward"},
{"step": "all"}
])
ts_vizu_selectors.plot(
series=[ts_1],
anomalies=[2, 10],
plot_title=plot_title,
y_title=y_title
)
import src.visualisations.plotly_time_series as TS
reload(TS)
ts_visu = TS.PlotlyTimeSeries()
ts_visu.plot(
series=[ts_1],
plot_title=plot_title,
y_title=y_title
)
import src.visualisations.plotly_time_series as TS
reload(TS)
ts_visu = TS.PlotlyTimeSeries()
ts_visu.plot(
series=[ts_1, ts_2, ts_3],
series_names=["Random Sinus", "Another Random Sinus", "Wooow"],
series_obs_names=[ts_1_names, ts_2_names, ts_3_names],
plot_title=plot_title,
y_title=y_title
)
import src.visualisations.plotly_time_series as TS
reload(TS)
ts_visu = TS.PlotlyTimeSeries()
ts_visu.plot(
series=[ts_1, ts_1+1, ts_1-2],
plot_title=plot_title,
y_title=y_title,
fill_areas=True
)
import src.visualisations.plotly_time_series as TS
reload(TS)
ts_visu = TS.PlotlyTimeSeries()
ts_visu.plot(
series=[ts_1, ts_2],
series_names=["Random Sinus", "Another Random Sinus"],
series_obs_names=[ts_1_names, ts_2_names],
anomalies=[2, 10],
anomalies_obs_names=["False", "Perfect"],
plot_title=plot_title,
y_title=y_title
)
ts_1 = create_time_series(seed_number=11, x_multiplier=0.3)
ts_1_names = [f"Amazing Data {i}" for i in range(1, len(ts_1)+1)]
ts_2 = create_time_series(seed_number=32, x_multiplier=1.1)
ts_2_names = [f"Random Data {i}" for i in range(1, len(ts_2)+1)]
buys = ts_1.iloc[[1, 9, 10, 12, 13, 14],]
buy_names = [f"Buy {i+1}" for i, _ in enumerate(buys)]
sells = ts_1.iloc[[4, 17, 20, 22, 26],]
sell_names = [f"Sell {i+1}" for i, _ in enumerate(sells)]
print(type(ts_1))
print(type(ts_1[0]))
print(type(ts_1.index))
print("\n")
print(ts_1.head())
plot_title = "Awesome Random Time Series Plot"
y_title = "Awesome Random Data"
<class 'pandas.core.series.Series'> <class 'numpy.float64'> <class 'pandas.core.indexes.datetimes.DatetimeIndex'> 2020-01-01 16.749455 2020-01-02 16.191528 2020-01-03 17.338647 2020-01-04 16.263316 2020-01-05 19.651911 dtype: float64
import src.visualisations.plotly_time_series_events as TS
reload(TS)
ts_visu = TS.PlotlyTimeSeriesEvents()
ts_visu.plot(
series=[ts_1],
events=[buys, sells],
plot_title=plot_title,
y_title=y_title
)
import src.visualisations.plotly_time_series_events as TS
reload(TS)
ts_visu = TS.PlotlyTimeSeriesEvents()
ts_visu.plot(
series=[ts_1, ts_2],
series_names=["Wonder Series", "Normal Series"],
series_obs_names=[ts_1_names, ts_2_names],
events=[buys, sells],
event_names=["Buys", "Sells"],
event_obs_names=[buy_names, sell_names],
plot_title=plot_title,
y_title=y_title
)
df_1 = DataFrame(create_time_series(seed_number=11, x_multiplier=0.3))
df_1.rename(columns={0: ATTR_OPEN.name}, inplace=True)
df_1[ATTR_HIGH.name] = create_time_series(seed_number=7651, x_multiplier=0.2)
df_1[ATTR_LOW.name] = create_time_series(seed_number=751, x_multiplier=0.4)
df_1[ATTR_CLOSE.name] = create_time_series(seed_number=76, x_multiplier=0.1)
df_1.head(2)
| OPEN | HIGH | LOW | CLOSE | |
|---|---|---|---|---|
| 2020-01-01 | 16.749455 | 14.744998 | 14.676495 | 15.940301 |
| 2020-01-02 | 16.191528 | 15.904529 | 16.832635 | 14.941537 |
ts_1 = create_time_series(seed_number=11, x_multiplier=0.3)
ts_1_names = [f"Amazing Data {i}" for i in range(1, len(ts_1)+1)]
ts_2 = create_time_series(seed_number=32, x_multiplier=1.1)
ts_2_names = [f"Random Data {i}" for i in range(1, len(ts_2)+1)]
buy_series = df_1["CLOSE"][[1, 27]]
buy_series_names = ["Good Buy", "Bad Buy"]
sell_series = df_1["CLOSE"][[6, 16]]
sell_series_names = ["So So Sell", "Wooow Sell"]
third_series = df_1["HIGH"][[10, 20]]
third_series_names = ["Test", "Next Test"]
plot_title = "Awesome Random Time Series Plot"
y_title = "Awesome Random Data"
import src.visualisations.plotly_candle as PC
reload(PC)
candle_visu = PC.PlotlyCandle()
candle_visu.plot(
df_market=df_1,
plot_title=plot_title,
y_title=y_title
)
import src.visualisations.plotly_candle as PC
reload(PC)
candle_visu = PC.PlotlyCandle()
candle_visu.plot(
df_market=df_1,
series=[ts_1, ts_2],
series_names=["MA_long", "MA_short"],
series_obs_names=[ts_1_names, ts_2_names],
plot_title=plot_title,
y_title=y_title
)
import src.visualisations.plotly_candle as PC
reload(PC)
candle_visu = PC.PlotlyCandle()
candle_visu.plot(
df_market=df_1,
series=[ts_1, ts_2],
series_names=["MA_long", "MA_short"],
series_obs_names=[ts_1_names, ts_2_names],
events=[buy_series, sell_series, third_series],
event_names=["Buy", "Sell", "Third"],
event_obs_names=[buy_series_names, sell_series_names, third_series_names],
plot_title=plot_title,
y_title=y_title
)
datetime(2020, 1, 12)
datetime.datetime(2020, 1, 12, 0, 0)
import src.visualisations.plotly_candle as PC
reload(PC)
candle_visu = PC.PlotlyCandle()
candle_visu.plot(
df_market=df_1,
series=[ts_1, ts_2],
series_names=["MA_long", "MA_short"],
series_obs_names=[ts_1_names, ts_2_names],
events=[buy_series, sell_series, third_series],
event_names=["Buy", "Sell", "Third"],
event_obs_names=[buy_series_names, sell_series_names, third_series_names],
vertical_lines_positions=[df_1.index[10], datetime(2020, 1, 15)],
plot_title=plot_title,
y_title=y_title
)
n = 100
x1 = randn(n)-2
x2 = randn(n+20)
x3 = randn(n+100)+2
x4 = randn(n+200)+4
x5 = randn(n+200)+6
x6 = randn(n+200)+8
data = [x1, x2, x3, x4, x5, x6]
group_labels = ["My Group 1", "My Group 2", "My Group 3", "My Group 4", "My Group 5", "My Group 6"]
bin_size = [0.1, 0.25, 0.5, 1, 0.125, 0.75]
plot_title = "My Super Random Histogram"
x_title = "Awesome Random Data"
import src.visualisations.plotly_histogram_distplot as HIST_DIST
reload(HIST_DIST)
histogram_distplot = HIST_DIST.PlotlyHistogramDistplot()
histogram_distplot.plot(data=data, plot_title=plot_title, x_title=x_title, group_labels=group_labels, bin_size=bin_size,
x_axis_min_max=None, vertical_lines_positions=None, dashboard=False)
import src.visualisations.plotly_histogram_distplot as HIST_DIST
reload(HIST_DIST)
histogram_distplot = HIST_DIST.PlotlyHistogramDistplot()
histogram_distplot.plot(data=data, plot_title=plot_title, x_title=x_title, group_labels=group_labels, bin_size=bin_size,
x_axis_min_max=[0, 4], vertical_lines_positions=None, dashboard=False)
import src.visualisations.plotly_histogram_distplot as HIST_DIST
reload(HIST_DIST)
histogram_distplot = HIST_DIST.PlotlyHistogramDistplot()
histogram_distplot.plot(data=data, plot_title=plot_title, x_title=x_title, group_labels=group_labels, bin_size=bin_size,
x_axis_min_max=None, vertical_lines_positions=[-1.7, 0.75, 6.74], dashboard=False)
n = 100
x1 = randn(n)-2
x2 = randn(n+20)
x3 = randn(n+100)+2
x4 = randn(n+200)+4
x5 = randn(n+200)+6
x6 = randn(n+200)+8
data = [x1, x2, x3, x4, x5, x6]
group_labels = ["My Group 1", "My Group 2", "My Group 3", "My Group 4", "My Group 5", "My Group 6"]
plot_title = "My Super Random Histogram"
x_title = "Awesome Random Data"
import src.visualisations.plotly_histogram_multi as HIST_MULTI
reload(HIST_MULTI)
histogram_multi = HIST_MULTI.PlotlyHistogramMulti()
histogram_multi.plot(data=data, plot_title=plot_title, x_title=x_title, group_labels=group_labels)
import src.visualisations.plotly_histogram_multi as HIST_MULTI
reload(HIST_MULTI)
histogram_multi = HIST_MULTI.PlotlyHistogramMulti()
histogram_multi.plot(data=data, plot_title=plot_title, x_title=x_title, group_labels=group_labels,
x_axis_min_max=(-7, 15), vertical_lines_positions=[-2, 0.5, 5.8])
x_list = list(range(1, 26))
x = array(x_list)
y_1 = array([0.5 * x for x in x_list])
y_2 = array([log(x) for x in x_list])
x_short = array(list(range(5, 16)))
y_short = x_short * 2
line_names = ["Linear", "Logarithm", "Shorter Line"]
plot_title = "Line Chart"
x_title = "X Values"
y_title = "Y Values"
import src.visualisations.plotly_line_chart as LINE
reload(LINE)
line_chart = LINE.PlotlyLineChart()
line_chart.plot(lines=[(x, y_1), (x, y_2)], plot_title=plot_title, x_title=x_title, y_title=y_title)
import src.visualisations.plotly_line_chart as LINE
reload(LINE)
line_chart = LINE.PlotlyLineChart()
line_chart.plot(lines=[(x, y_1), (x, y_2), (x_short, y_short)], line_names=line_names, plot_title=plot_title, x_title=x_title, y_title=y_title)
seed(185)
data = normal(size=500)
print(f"Type of the data is: {type(data)}")
print(f"Several observations: {data[0:10]}")
plot_title = "My Super Random Histogram"
x_title = "Awesome Random Data"
Type of the data is: <class 'numpy.ndarray'> Several observations: [ 0.04471179 -1.09776817 1.63620698 -1.12098251 -0.93842551 -0.71691279 0.77317168 0.31091126 0.67555112 1.00791911]
import src.visualisations.plotly_histogram as HIST
reload(HIST)
histogram = HIST.PlotlyHistogram()
histogram.plot(data=data, plot_title=plot_title, x_title=x_title)
import src.visualisations.plotly_histogram as HIST
reload(HIST)
histogram = HIST.PlotlyHistogram()
histogram.customize_size(autosize=False, width=2000, height=1500)
histogram.plot(data=data, plot_title=plot_title, x_title=x_title)
Logger().end_timer()
2023-12-10 16:11:11,394 - file_limit_console - INFO - Process: NOTEBOOK; Notebook name: visualisation_documentation.py; Timer ended; Process Duration [s]: 2.36; Process Duration [m]: 0.04